package cn.iocoder.yudao.module.shop.service.shopSettleApply;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.shop.controller.admin.shopSettleApply.vo.ShopSettleApplyPageReqVO;
import cn.iocoder.yudao.module.shop.controller.admin.shopSettleApply.vo.ShopSettleApplySaveReqVO;
import cn.iocoder.yudao.module.shop.dal.dataobject.shopSettleApply.ShopSettleApplyDO;
import cn.iocoder.yudao.module.shop.dal.mysql.shopSettleApply.ShopSettleApplyMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.SHOP_SETTLE_APPLY_NOT_EXISTS;

/**
 * 商户入驻申请 Service 实现类
 *
 * @author 吕小文
 */
@Service
@Validated
public class ShopSettleApplyServiceImpl implements ShopSettleApplyService {

    @Resource
    private ShopSettleApplyMapper shopSettleApplyMapper;

    @Override
    public Long createShopSettleApply(ShopSettleApplySaveReqVO createReqVO) {
        // 插入
        ShopSettleApplyDO shopSettleApply = BeanUtils.toBean(createReqVO, ShopSettleApplyDO.class);
        shopSettleApplyMapper.insert(shopSettleApply);
        // 返回
        return shopSettleApply.getId();
    }

    @Override
    public void updateShopSettleApply(ShopSettleApplySaveReqVO updateReqVO) {
        // 校验存在
        validateShopSettleApplyExists(updateReqVO.getId());
        // 更新
        ShopSettleApplyDO updateObj = BeanUtils.toBean(updateReqVO, ShopSettleApplyDO.class);
        shopSettleApplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteShopSettleApply(Long id) {
        // 校验存在
        validateShopSettleApplyExists(id);
        // 删除
        shopSettleApplyMapper.deleteById(id);
    }

    private void validateShopSettleApplyExists(Long id) {
        if (shopSettleApplyMapper.selectById(id) == null) {
            throw exception(SHOP_SETTLE_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public ShopSettleApplyDO getShopSettleApply(Long id) {
        return shopSettleApplyMapper.selectById(id);
    }

    @Override
    public PageResult<ShopSettleApplyDO> getShopSettleApplyPage(ShopSettleApplyPageReqVO pageReqVO) {
        return shopSettleApplyMapper.selectPage(pageReqVO);
    }

}